<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <!-- 请购人（模糊查询），单据编号，需求日期（开始，结束），采购类型，公司部门（级联选择框） -->
        <el-form-item>
          <el-input v-model="filters.psnname" placeholder="请购人"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.orderNum" placeholder="单据编号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="filters.needDayStart"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="需求开始日期"
          ></el-date-picker>-
          <el-date-picker
            v-model="filters.needDayStop"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="需求结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select v-model="filters.buyType" placeholder="采购类型">
            <el-option label="紧急采购" value="0"></el-option>
            <el-option label="零星采购" value="1"></el-option>
            <el-option label="合同采购" value="2"></el-option>
          </el-select>
          <el-form-item>
            <el-date-picker
                    v-model="filters.submitTimeStart"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="已购买提交开始日期"
            ></el-date-picker>-
            <el-date-picker
                    v-model="filters.submitTimeStop"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="已购买提交结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-form-item>
        <!-- <el-form-item>
          <companyDepart @getCompanyDepart="getCompanyDepart"></companyDepart>
        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" v-on:click="getList">查询</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>-->
      </el-form>
    </el-col>
    <!--列表-->
    <el-table
      :data="formList"
      highlight-current-row
      v-loading="listLoading"
      @selection-change="selsChange"
      style="width: 100%;"
      height="50vh"
    >
      <!-- 单据编号，请购日期，需求日期，产品名称规格型号，采购类型，公司，部门，请购人，制单人，待我询价 -->
      <el-table-column prop="orderNum" label="单据编号" align="center"></el-table-column>
      <!-- <el-table-column prop="uploadDay" label="请购日期" align="center"></el-table-column> -->
      <!-- <el-table-column prop="needDay" label="需求日期" align="center"></el-table-column> -->
      <!-- <el-table-column prop="custname" label="产品名称规格型号" width="120"></el-table-column> -->
      <el-table-column prop="buyType" label="采购类型" :formatter="formatterBuyType"></el-table-column>
      <!-- <el-table-column prop="unitname" label="公司"></el-table-column> -->
      <el-table-column prop="deptname" label="部门"></el-table-column>
      <el-table-column prop="psnname" label="请购人"></el-table-column>
      <el-table-column prop="singlePerson" label="制单人"></el-table-column>
      <el-table-column prop="submitTime" label="已购买提交日期" :formatter="formatTime"></el-table-column>
      <el-table-column prop="type" label="订单状态" :formatter="orderType"></el-table-column>
      <!-- <el-table-column prop="singlePerson" label="待我询价"></el-table-column> -->
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">详情</el-button>
          <!-- <el-button type="danger" size="small" @click="deleteItem(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="page.pageSize"
        :total="total"
        style="float:right;"
      ></el-pagination>
    </el-col>
    <!--编辑界面-->
    <el-dialog title="修改询价单" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <inquiryForm></inquiryForm>
    </el-dialog>
    <!--新增界面-->
    <el-dialog title="新增询价单" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <inquiryForm></inquiryForm>
    </el-dialog>
  </section>
</template>

<script>
import util from "../../common/js/util";
import {
  wlcSysInquiryRemove,
  wlcSysInquiryWlcSysInquiryLastList,
  WlcPurchaseList,
} from "../../api/api";
import moment from "moment";
export default {
  components: {},
  data() {
    return {
      filters: {
        psnname: "",
        orderNum: "",
        needDayStart: "",
        needDayStop: "",
        buyType: "",
        unitnameId: "",
        deptnameId: "",
        submitTimeStart: "",
        submitTimeStop: "",
      },
      formList: [],
      total: 0,
      page: {
        page: 1,
        pageSize: 10,
      },
      listLoading: false,
      sels: [], //列表选中列

      editFormVisible: false, //编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
      },
      //编辑界面数据
      editForm: {
        id: 0,
        name: "",
        sex: -1,
        age: 0,
        birth: "",
        addr: "",
      },

      addFormVisible: false, //新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
      },
      //新增界面数据
      addForm: {
        name: "",
        sex: -1,
        age: 0,
        birth: "",
        addr: "",
      },
    };
  },
  methods: {
    getCompanyDepart(e) {
      this.filters.unitnameId = e[2].value;
      this.filters.deptnameId = e[3].pkDeptdoc;
    },
    //格式化时间
    formatTime(time) {
      if (time) {
        return moment(time).format("YYYY-MM-DD");
      } else {
        return "";
      }
    },
    //格式化采购类型
    formatterBuyType(e) {
      let buyType = e.buyType;
      let text = "";
      switch (buyType) {
        case '0':
          text = "紧急采购";
          break;
        case '1':
          text = "零星采购";
          break;
        case '2':
          text = "合同采购";
          break;
        default:
          text = "";
      }
      return text;
    },
    //单据状态
    orderType(e) {
      let type = e.type;
      let text = "";
      switch (type) {
        case '2':
          text = "待采购";
          break;
        case '9':
          text = "采购结束";
          break;
        default:
          text = "";
      }
      return text;
    },
    //单独删除
    deleteItem(item) {
      let arr = [];
      let obj = {
        id: item.id,
      };
      arr.push(obj);
      this.handleDel(arr);
    },
    handleCurrentChange(val) {
      this.page.page = val;
      this.getList();
    },
    //查看比较单
    seeContrastForm() {
      this.$router.push({ path: "productContrast" });
    },
    //获取列表
    getList() {
      let para = Object.assign({}, this.filters);
      para.page = this.page;
      this.listLoading = true;
      WlcPurchaseList(para).then((res) => {
        this.total = res.total;
        this.formList = res.records;
        this.listLoading = false;
      });
    },
    //删除
    handleDel: function (arr) {
      this.$confirm("确认删除该记录吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          this.listLoading = true;
          //NProgress.start();
          wlcSysInquiryRemove(arr).then((res) => {
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.getList();
          });
        })
        .catch(() => {});
    },
    //显示编辑界面
    handleEdit(index, row) {
      this.$router.push({
        path: "buyOrderDetail",
        query: {
          id: row.id,
        },
      });
    },
    //显示新增界面
    handleAdd: function () {
      this.$router.push({ path: "purSumList" });
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.editForm);
            para.birth =
              !para.birth || para.birth == ""
                ? ""
                : util.formatDate.format(new Date(para.birth), "yyyy-MM-dd");
            editUser(para).then((res) => {
              this.editLoading = false;
              //NProgress.done();
              this.$message({
                message: "提交成功",
                type: "success",
              });
              this.$refs["editForm"].resetFields();
              this.editFormVisible = false;
              this.getList();
            });
          });
        }
      });
    },
    //新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.addLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.addForm);
            para.birth =
              !para.birth || para.birth == ""
                ? ""
                : util.formatDate.format(new Date(para.birth), "yyyy-MM-dd");
            addUser(para).then((res) => {
              this.addLoading = false;
              //NProgress.done();
              this.$message({
                message: "提交成功",
                type: "success",
              });
              this.$refs["addForm"].resetFields();
              this.addFormVisible = false;
              this.getList();
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map((item) => item.id).toString();
      this.$confirm("确认删除选中记录吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = { ids: ids };
          batchRemoveUser(para).then((res) => {
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.getList();
          });
        })
        .catch(() => {});
    },
  },
  mounted() {
    this.getList();
    // this.getCompany();
  },
};
</script>
<style lang="less" scoped>
</style>
